package com.jonas.schart;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.SeekBar;

import com.jonas.jgraph.graph.NChart;
import com.jonas.jgraph.models.NExcel;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 第一个 简单的activity
 */
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    private SeekBar mSbBarwidth;  //第一个进度条滑块 控制柱形图宽度
    private SeekBar mSbBarinterval;  //第二个控制柱形图之间的宽度
    private SeekBar mSbHcoordinate;   //第三个控制底边实线与控件底边的距离
    private SeekBar mSbmabove;   //第四条 控制柱状图与底部画线的距离
    private NChart mChart;        //统计显示控件

    private boolean style_bar = true;  //用于切换柱状图和线性图

    /**
     * 四个进度条的初始化
     */
    private void assignViews(){
        mSbBarwidth = (SeekBar)findViewById(R.id.sb_barwidth);
        mSbBarinterval = (SeekBar)findViewById(R.id.sb_barinterval);
        mSbHcoordinate = (SeekBar)findViewById(R.id.sb_hcoordinate);
        mSbmabove = (SeekBar)findViewById(R.id.sb_mabove);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mChart = (NChart)findViewById(R.id.sug_recode_schar);

        List<NExcel> nExcelList = new ArrayList<>();  //用于存储生成的随机数数据

        Random random = new Random();  //随机数生成器
        for(int i = 0; i<8; i++) {
            nExcelList.add(new NExcel(80+random.nextInt(100), "测试"));  //80--180  //8条数据
        }
        nExcelList.add(new NExcel(99, 150, "测试"));  //第9条数据

//        Color.parseColor("#ffe9d1ba");
//        mChart.setScrollAble(false);
//        mChart.setFixedWidth(50);
        mChart.setBarStanded(7);
        mChart.setBarAniStyle(NChart.ChartAniStyle.BAR_RIGHT);
//        mChart.setExecelPaintShaderColors(new int[]{Color.parseColor("#089900"), Color.parseColor("#9FC700")});
//        mChart.setExecelPaintShaderColors(new int[]{Color.parseColor("#4df1dbd4"), Color.TRANSPARENT});
        mChart.setNormalColor(Color.parseColor("#089900"));  //设置柱型图颜色
        mChart.cmdFill(nExcelList);  //传入数据列表
        assignViews();  //控件关联初始化
        setListeners();  //SeekBar关联初始化
    }

    private void setListeners(){
        mSbBarwidth.setOnSeekBarChangeListener(this);
        mSbBarwidth.setProgress((int)mChart.getBarWidth());  //矩形柱宽度
        mSbBarinterval.setOnSeekBarChangeListener(this);
        mSbBarinterval.setProgress((int)mChart.getInterval());  //矩形柱间间隔
        mSbHcoordinate.setOnSeekBarChangeListener(this);
        mSbHcoordinate.setProgress((int)mChart.getHCoordinate());  //线轴据底部位置
        mSbmabove.setOnSeekBarChangeListener(this);
        mSbmabove.setProgress((int)mChart.getAbove());  //x轴 柱状图 在x轴上方 的距离
    }

    /**
     * 各个进度条滑动时的回掉
     * @param seekBar
     * @param progress
     * @param fromUser
     */
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
        switch(seekBar.getId()) {
            case R.id.sb_barwidth:
                mChart.setBarWidth(progress);  //第一个进度条滑块 控制柱形图宽度
                break;
            case R.id.sb_barinterval:
                mChart.setInterval(progress);   //第二个控制柱形图之间的宽度
                break;
            case R.id.sb_hcoordinate:
                mChart.setHCoordinate(progress);   //第三个控制底边实线与控件底边的距离
                break;
            case R.id.sb_mabove:
                mChart.setAbove(progress);  //第四条 控制柱状图与底部画线的距离
                break;
        }
        mChart.postInvalidate();  //更新绘画
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar){

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar){

    }
    //在圆柱形和折线图切换
    public void stylechange(View v){
        mChart.setChartStyle(( style_bar = !style_bar ) ? NChart.ChartStyle.BAR : NChart.ChartStyle.LINE);
        mChart.postInvalidate();
    }
    //动画再执行一遍
    public void animate(View v){
        mChart.animateShow();
    }

    /**
     * 数字显示或者隐藏
     * @param v
     */
    public void selecterChange(View v){
        if (mChart.getSelectedModed()== NChart.SelectedMode.selecetdMsgShow) {
            mChart.setSelectedModed(NChart.SelectedMode.selectedActivated);  //显示数字
        } else {
            mChart.setSelectedModed(NChart.SelectedMode.selecetdMsgShow);  //隐藏数字
        }
    }

}
